package top.hzy520.admin.user.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import top.hzy520.admin.common.constants.Constants;
import top.hzy520.admin.common.util.AliyunUploadDown;
import top.hzy520.admin.user.dao.UserMapper;
import top.hzy520.admin.user.dto.UserUpdateParam;
import top.hzy520.admin.user.entity.User;

import java.util.List;

/**
 * @Author: HouZeYu
 * @Description:
 * @Date: Created in 10:37 2019/6/3
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Override
    public String checkEmailUnique(Integer userId, String email) {
     User user=userMapper.selectByPrimaryKey(userId);
     if (user.getEmail()==null){
        return Constants.USER_EMAIL_UNIQUE;
     }else {
         List<User> userList=userMapper.selectUserByEmail(email);
         if (userList.size()==0){
             return Constants.USER_EMAIL_UNIQUE;
         }else {
             return Constants.USER_EMAIL_NOT_UNIQUE;
         }
     }

    }

    @Override
    public String checkPhoneUnique(Integer userId, String mobile) {
        List<User> userList=userMapper.selectUserByMobile(mobile);
        if (userList.size()==0){
            return Constants.USER_EMAIL_UNIQUE;
        }else {
            return Constants.USER_EMAIL_NOT_UNIQUE;
        }
    }

    @Override
    public String checkUsernameUnique(Integer userId, String username) {
        User user=userMapper.selectByUserName(username);
        if (user==null){
            return Constants.USER_EMAIL_UNIQUE;
        }else {
            return Constants.USER_EMAIL_NOT_UNIQUE;
        }
    }

    @Override
    public int profileUpdate(UserUpdateParam userUpdateParam) {
        User user=new User();
        user.setId(userUpdateParam.getUserId());
        user.setMobile(userUpdateParam.getMobile());
        user.setEmail(userUpdateParam.getEmail());
        user.setGender(userUpdateParam.getGender());
       return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public User selectUserByUsername(String username) {
        return userMapper.selectByUserName(username);
    }

    @Override
    public int profileResetPwd(UserUpdateParam userUpdateParam) {
        User user=new User();
        user.setId(userUpdateParam.getUserId());
        user.setPassword(passwordEncoder.encode(userUpdateParam.getNewPassword()));
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public int updateAvatar(Integer id, MultipartFile file) {
        try {
            User user=new User();
            String imgUrl=AliyunUploadDown.picOSS(file);
            user.setId(id);
            user.setHeader(imgUrl);
           return userMapper.updateByPrimaryKeySelective(user);
        } catch (Exception e) {
            log.error("用户ID: {},图片上传出错",id);
            return 0;
        }
    }
}
